package dao

import (
	"gitee.com/mangenotwork/game-douniu/api/common"
	"gitee.com/mangenotwork/game-douniu/api/entity"
	"gorm.io/gorm"
)

type CoinDao struct {
}

// 获取批量用户游戏币
func (dao *CoinDao) GetByUsers(userIds []int64) (map[int64]*entity.UserCoin, error) {
	db := common.GetGorm("test")
	result := make([]*entity.UserCoin, 0)
	resultMap := make(map[int64]*entity.UserCoin)
	err := db.Model(&entity.UserCoin{}).Where("user_id in (?)", userIds).Find(&result).Error
	if err == nil {
		for _, v := range result {
			resultMap[v.UserId] = v
		}
	}
	return resultMap, err
}

// 用户增减游戏币
func (dao *CoinDao) UserCoinAdd(userId, coin int64) error {
	db := common.GetGorm("test")
	db = db.Model(&entity.UserCoin{}).Where("user_id=?", userId)
	if coin > 0 {
		db = db.Update("balance", gorm.Expr("balance + ?", coin))
	} else {
		db = db.Update("balance", gorm.Expr("balance - ?", coin))
	}
	return db.Error
}

// 用户申请游戏币
func (dao *CoinDao) ApplyCoin(data *entity.UserApplyCoin) error {
	db := common.GetGorm("test")
	return db.Model(&entity.UserApplyCoin{}).Create(data).Error
}

// 获取游戏币申请列表
func (dao *CoinDao) ApplyCoinList(page, limit int64) (int64, []*entity.UserApplyCoin, error) {
	db := common.GetGorm("test")
	result := make([]*entity.UserApplyCoin, 0)

	db = db.Model(&entity.UserApplyCoin{}).Where("state == 0")

	var count int64 = 0
	err := db.Count(&count).Error
	if err != nil {
		return count, result, err
	}

	err = db.Order("id desc").Scopes(Pagination(int(page), int(limit))).Find(&result).Error
	return count, result, err
}

// 同意用户申请游戏币
func (dao *CoinDao) AgreeCoin() {}

// 拒绝用户申请游戏币
func (dao *CoinDao) RejectCoin() {}

// 查看用户游戏币使用记录
func (dao *CoinDao) GetUserCoinLogList() {}

// 记录用户游戏币使用
func (dao *CoinDao) SetUserCoinLog() {}
